/**
 * 知识库的JS方法
 */
XF.WIKI = new function(){

	/**
	 * 读取目录
	 */
	this.view = function(folderId){
		XF.lightSideBar('view_wiki'); 
		var url  = global.contextPath + "/wiki/view"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {folderId:folderId},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
			  //高度
			  autoHeight();
			  $(window).resize(function(){
				  autoHeight();
			  });
			  //绑定Tree事件
			  XF.WIKI.Tree("#noticeCenter");
		  }
		});
		function autoHeight(){
			$("#autowikiHeight").height($(window).height()-73);
		}
	};
	
	/**
	 * 读取文章
	 */
	this.viewItem = function(itemId){
		XF.lightSideBar('view_wiki'); 
		var url  = global.contextPath + "/wiki/view/item"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {itemId:itemId},
		  complete: function(xhr){
			   xhr = xhr['responseText'];
			  $("#wikiContentPanel").html(xhr);
			  /*autoWidth();
			  $(window).resize(function(){
				  autoWidth();
			  });
			  $("#noticeCenter").css({"overflow":"hidden"});*/
		  }
		});
		function autoWidth(){
			$("#wikibodyHeight").height($(window).height()-73).width($(window).width()-500);
		}
	};
	
	
	/**
	 * 目录管理-全目录
	 */
	this.catalog = function(){
		XF.lightSideBar('view_catalog'); 
		var url  = global.contextPath + "/wiki/catalog"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
			  //绑定Tree事件
			  XF.WIKI.Tree(".wikiFolderPanel");
		  }
		});
		
	};
	
	/**
	 * 修改目录
	 */
	this.catalog_edit = function(folderId){
		XF.lightSideBar('view_catalog'); 
		var url  = global.contextPath + "/wiki/catalog/edit"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {folderId:folderId},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  xhr = new Function("return " + xhr);
			  xhr = xhr();
			  var title = xhr['title'];
			  $("#win_catalog").attr("sid",folderId).attr("stitle",title);
			  $("#win_catalog input[name=newTitle]").val(title);
			  $('#win_catalog').window('open');  
		  }
		});
	};
	
	/**
	 * 修改目录-执行
	 */
	this.catalog_doEdit = function(){
		XF.lightSideBar('view_catalog'); 
		var url  = global.contextPath + "/wiki/catalog/doEdit"
		var newTitle = $("input[name=newTitle]").val();
		var sid = $("#win_catalog").attr("sid");
		$.ajax({
		  url: url,
		  type: "post",
		  data: {
			  folderId:sid,
			  title:newTitle
			  },
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  xhr = new Function("return " + xhr);
			  xhr = xhr();
			  var status = xhr['status'];
			  if("SUCCESS"==status){
				  //关闭弹出层
				  $("#win_catalog").attr("sid","").attr("stitle","");
				  $('#win_catalog').window('close');  
				  $.messager.show({
					title:'提示',
					msg:'修改成功',
					timeout:5000,
					showType:'slide'
				  });
				  //显示在页面
				  var newT = xhr['newTitle'];
				  var sid = xhr['sid'];
				  $(".wikiFolderPanel .file a[sid='"+sid+"']").html(newT);
			  }
		  }
		});
	};
	
	/**
	 * 添加子目录
	 */
	this.catalog_addSub = function(folderId){
		XF.lightSideBar('view_catalog'); 
		var url  = global.contextPath + "/wiki/catalog/addSub"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {folderId:folderId},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  xhr = new Function("return " + xhr);
			  xhr = xhr();
			  $("#win_catalog_add").attr("sid",folderId);
			  $('#win_catalog_add').window('open');  
		  }
		});
	};
	
	/**
	 * 添加子目录-执行
	 */
	this.catalog_doAddSub = function(){
		XF.lightSideBar('view_catalog'); 
		var url  = global.contextPath + "/wiki/catalog/doAddSub"
		var newTitle = $("input[name=newTitle_add]").val();
		var sid = $("#win_catalog_add").attr("sid");
		$.ajax({
		  url: url,
		  type: "post",
		  data: {
			  folderId:sid,
			  title:newTitle
			  },
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  xhr = new Function("return " + xhr);
			  xhr = xhr();
			  var status = xhr['status'];
			  if("SUCCESS"==status){
				  //关闭弹出层
				  $("#win_catalog_add").attr("sid","").attr("stitle","");
				  $("input[name=newTitle_add]").val("");
				  $('#win_catalog_add').window('close');  
				  $.messager.show({
					title:'提示',
					msg:'添加成功',
					timeout:5000,
					showType:'slide'
				  });
			  }
		  }
		});
	};
	
	
	/**
	 * 添加条目
	 */
	this.item_add = function(){
		XF.lightSideBar('item_add'); 
		var url  = global.contextPath + "/wiki/item/add"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 条目管理页面列表-默认页
	 */
	this.item_manage = function(){
		XF.lightSideBar('item_manage'); 
		_wiki_search(null,null,"","");
		/*var url  = global.contextPath + "/wiki/item/manage"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
		  }
		});*/
	};
	
	/**
	 * 条目管理页面列表-搜索页
	 */
	this.item_manage_search = function(){
		var title=$("input[name=title]").val();
		var folderId = $("select[name=folderId]").val();
		XF.lightSideBar('item_manage');
		_wiki_search(null,null,title,folderId);
	}
	
	
	/**
	 * 编辑条目
	 */
	this.item_edit = function(id){
		XF.lightSideBar('item_manage'); 
		var url  = global.contextPath + "/wiki/item/edit"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {id:id},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
		  }
		});
	};
	
	/**
	 * 删除条目
	 */
	this.item_delete = function(id){
		$.messager.confirm('提示',"确认删除这篇文章？",function(r){
		    if (r){
				var url  = global.contextPath + "/wiki/item/doDelete"
				$.ajax({
				  url: url,
				  type: "post",
				  data: {id:id},
				  complete: function(xhr){
					  xhr = xhr['responseText'];
					  xhr = new Function("return " + xhr);
					  xhr = xhr();
					  xhr = xhr['status'];
					  if(xhr=="SUCCESS"){
						  $.messager.show({
							title:'提示',
							msg:'删除成功',
							timeout:3000,
							showType:'slide'
						  });
						  $(".userListWrap tr i[s="+id+"]").parent().parent().parent().remove();
					  }else if(xhr=="FAILD"){
						  $.messager.show({
							title:'提示',
							msg:'删除失败',
							timeout:5000,
							showType:'slide'
						  });
					  }
				   }
				});
		    }
		});
	};
	
	
	/**
	 * 文章搜索列表页
	 */
	this.view_search = function(folderId,title){
		XF.lightSideBar('view_wiki'); 
		if(typeof(folderId)=="undefined" && typeof(title)=="undefined"){
			folderId = null;
			title = $("input[name=keywords]").val();
		}
		if(typeof(folderId)!="undefined" && typeof(title)=="undefined"){
			title = null;
		}
		_wiki_view_search(null,null,title,folderId);
	}
	
	
	
	/**
	 * 审核条目
	 */
	this.audit = function(id){
		XF.lightSideBar('audit'); 
		_wiki_audit_search(null,null,null,null);
		
		/*var url  = global.contextPath + "/wiki/item/audit"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
		  }
		});*/
	};
	
	/**
	 * 审核预览
	 */
	this.viewItem4Audit = function(id){
		XF.lightSideBar('audit'); 
		var url  = global.contextPath + "/wiki/item/audit/view"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {itemId:id},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  $("#noticeCenter").html(xhr);
		  }
		});
	}
	
	/**
	 * 审核-执行
	 */
	this.doAudit = function(r,id){
		XF.lightSideBar('audit'); 
		var url  = global.contextPath + "/wiki/item/doAudit"
		$.ajax({
		  url: url,
		  type: "post",
		  data: {itemId:id,auditStatus:r},
		  complete: function(xhr){
			  xhr = xhr['responseText'];
			  xhr = new Function("return " + xhr);
			  xhr = xhr();
			  var status = xhr['status'];
			  if("SUCCESS"==status){
				  var msg = xhr['msg'];
				  $.messager.show({
					title:'提示',
					msg:msg,
					timeout:5000,
					showType:'slide'
				  });
			  }
		  }
		});
	}

	
	/**
     * Tree插件
     */
	this.Tree = function(o){
		var val = {};
		
		val.box = $(o);
		val.ul = val.box.find("ul");
		val.li = val.box.find("li");
		val.classA = "open";
		val.count = val.box.length;
		//基本样式
		val.li.each(function(index){
			val.t = $(this);
			if(val.t.has("ul").length>0)
			{
				if(index+1==val.count){
					val.classA = "fopen press";
				}else if(index == 0){
					val.classA = "lopen press";
				}else{
					val.classA = "open press";
				}
				val.t.children("div").find("i:eq(0)").removeClass("switch").addClass(val.classA).next().removeClass("item").addClass("folder");
			}
		});
		//所有UI最后LI元素
		val.ul.each(function(index){
			val.t = $(this).children("li").last().children("div").find("i:eq(0)");
			if(val.t.hasClass("switch")){
				val.t.removeClass("switch").addClass("last");
			}else if(val.t.hasClass("open")){
				val.t.removeClass("open").addClass("lopen");
			}
		});
		//第一个UI最后LI元素
		val.t = val.box.find(".one").children("li").last();
		if(val.t.has("ul").length>0){
			val.t = val.t.children("ul").children("li");
			val.t.css({"background":"none"});
			if(val.t.last().has("ul").length>0){
				val.t.last().children("ul").children("li").css({"background":"none"});
			}
		}
		//绑定事件
		val.box.find(".press").on("click",function(){
			val.t = $(this);
			val.ul = $(this).parent().next();
			val.ul.slideToggle(200,function(){
				if(val.ul.is(":hidden")){
					if(val.t.hasClass("open"))
					{
						val.t.removeClass("open").addClass("off");
					}else if(val.t.hasClass("fopen")){
						val.t.removeClass("fopen").addClass("foff");
					}else if(val.t.hasClass("lopen")){
						val.t.removeClass("lopen").addClass("loff");
					}
				}else{
					if(val.t.hasClass("off"))
					{
						val.t.removeClass("off").addClass("open");
					}else if(val.t.hasClass("foff")){
						val.t.removeClass("foff").addClass("fopen");
					}else if(val.t.hasClass("loff")){
						val.t.removeClass("loff").addClass("lopen");
					}
				}
			});
		});
	};
	
    
    
    /**
     * wiki搜索方法
     */
    function _wiki_search(orderByField,orderByMode,title,folderId){
    	config.wiki_search_manage['args'] = [orderByField,orderByMode,title,folderId];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.wiki_search_manage);
    }
    
    
    /**
     * 文章列表搜索
     */
    function _wiki_view_search(orderByField,orderByMode,title,folderId){
    	config.wiki_view_search['args'] = [orderByField,orderByMode,title,folderId];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.wiki_view_search);
    }
    
    /**
     * 待审核列表
     */
    function _wiki_audit_search(orderByField,orderByMode,title,folderId){
    	config.wiki_audit_manage['args'] = [orderByField,orderByMode,title,folderId];
    	SMT.xconfig.gvar.pageNum = '1';
    	SMT.ajax(config.wiki_audit_manage);
    }
 
    
}();


config.wiki_search_manage = {
		url: ""+global.contextPath+"/wiki/item/manage",
	    argsOrder: ['orderByField','orderByMode','title','folderId'],
	    needVar: true,
	    replaceExp: '#noticeCenter',
	    callBack: function() {
	    }		
	};

config.wiki_view_search = {
		url: ""+global.contextPath+"/wiki/view/search",
	    argsOrder: ['orderByField','orderByMode','title','folderId'],
	    needVar: true,
	    replaceExp: '#wikiContentPanel',
	    callBack: function() {
	    }		
	};
config.wiki_audit_manage = {
		url: ""+global.contextPath+"/wiki/item/audit",
	    argsOrder: ['orderByField','orderByMode','title','folderId'],
	    needVar: true,
	    replaceExp: '#noticeCenter',
	    callBack: function() {
	    }		
	};
